<?php
/**
 * WP-ImgCode mod
 *
 * @package WP-ImgCode mod
 * @author C.H.Weng<br/>(Port here by <a href="http://xiazhengxin.name/">Sharl</a>)
 * @version 0.1
 * @link http://www.chweng.idv.tw/
 */

class wpimgcode_Plugin implements Typecho_Plugin_Interface{

	public static function activate(){
		Typecho_Plugin::factory('Widget_Feedback')->comment = array('wpimgcode_Plugin', 'verify_code');

		return _t('请将 &lt;?php wpimgcode_Plugin::show_code(); ?&gt; 代码放入你希望显示验证码的地方, 以使其生效');
	}

	public static function deactivate(){}

	public static function config(Typecho_Widget_Helper_Form $form){}

	public static function personalConfig(Typecho_Widget_Helper_Form $form){}

	public static function show_code(){
		$plugin_dir = Helper::options()->index.'/../usr/plugins/wpimgcode';
		echo '<tr><td><label for="imgcode">验证码：</label></td><td><input type="text" name="imgcode" /></td><td><img alt="（若看不到，请刷新。）" src="'.$plugin_dir.'/imgcode.php" /></td></tr>';
	}

	public function verify_code($commentdata) {
		global $comment_type;
		if ($comment_type == '' && (Typecho_Request::getInstance()->imgcode != $_SESSION['IMGCODE'] || time() >= $_SESSION['IMGCODE_EXPIRED'])) {
			unset($_SESSION['IMGCODE']);
			unset($_SESSION['IMGCODE_EXPIRED']);
			throw new Typecho_Widget_Exception(_t('验证码错误!'));
		}
		return $commentdata;
	}
}